package com.gudaomai.wuye.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gudaomai.wuye.common.CommonConstant;
import com.gudaomai.wuye.common.CommonException;
import com.gudaomai.wuye.common.MsgConstant;
import com.gudaomai.wuye.entity.HouseOwner;
import com.gudaomai.wuye.entity.HouseOwnerInfo;
import com.gudaomai.wuye.mapper.HouseOwnerMapper;
import com.gudaomai.wuye.service.IHouseOwnerServ;
import com.gudaomai.wuye.service.IHouseServ;
import com.gudaomai.wuye.service.impl.BaseServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zenghualu
 * @since 2017-09-01
 */
@Service
public class HouseOwnerServImp extends BaseServiceImpl<HouseOwnerMapper, HouseOwner> implements IHouseOwnerServ {

	@Autowired
	IHouseServ houseSevice;

	// 判断房主是否存在
	@Override
	public boolean isExist(String idHouse) {
		// TODO Auto-generated method stub
		return false;
	}

	// 通过住房id获取房主信息
	@Override
	public HouseOwner getHouseOwnerByHouseID(String id) throws CommonException {

		EntityWrapper<HouseOwner> ew = new EntityWrapper<>();
		ew.setEntity(new HouseOwner());
		ew.eq("id_house", id).notLike("is_delete", "0");

		HouseOwner houseOwner = this.selectOne(ew);

		return houseOwner;
	}

	// 通过车位id获取房主信息
	@Override
	public HouseOwner getHouseOwnerByParkID(String id) {

		// TODO

		return null;
	}

	// 根据房屋编号或者业主信息获取业主信息
	@Override
	public List<HouseOwnerInfo> getHouseOwnersInfo(String houseId, String ownerInfo) throws CommonException {

		String[] ownerInfos = ownerInfo.split(Character.toString(ownerInfo.charAt(ownerInfo.indexOf("1"))));

		List<HouseOwnerInfo> infos = null;

		if (ownerInfos.length > 1)
			infos = this.baseMapper.selectHouseOwnersInfo(houseId, ownerInfos[0].trim(), ownerInfos[1].trim());
		else
			infos = this.baseMapper.selectHouseOwnersInfo(houseId, ownerInfos[0].trim(), null);

		if (houseSevice.isExist(houseId) && !infos.isEmpty()) {
			return infos;
		} else {
			throw new CommonException(CommonConstant.RESULT_CODE_NOT_FOUND,
					getMessage(MsgConstant.RET_HOUSEOWNER_SELECT_FAILURE));
		}
	}

}
